#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定义双向链表的节点结构体
typedef struct Student {
    char name[50];
    int score;
    struct Student *prev;
    struct Student *next;
} Student;

// 函数声明
Student* createList(const char *filename);
void addStudent(Student **head, char *name, int score);
void deleteStudent(Student **head, char *name);
int findStudent(Student *head, char *name);
void printStudents(Student *head);
void saveToFile(Student *head, const char *filename);

// 创建新的学生节点
Student* createNode(char *name, int score) {
    Student *newNode = (Student*)malloc(sizeof(Student));
    if (newNode == NULL) {
        fprintf(stderr, "Memory allocation failed.\n");
        exit(1);
    }
    strcpy(newNode->name, name);
    newNode->score = score;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

// 从文件读取学生信息，并建立链表
Student* createList(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        perror("Error opening file");
        exit(1);
    }
    Student *head = NULL, *tail = NULL;
    char name[50], scoreStr[10];

    while (fscanf(file, "%s %s", name, scoreStr) == 2) {
        int score = atoi(scoreStr);
        Student *newNode = createNode(name, score);
        if (head == NULL) {
            head = newNode;
        } else {
            tail->next = newNode;
            newNode->prev = tail;
        }
        tail = newNode;
    }
    fclose(file);
    return head;
}

// 添加学生信息
void addStudent(Student **head, char *name, int score) {
    Student *newNode = createNode(name, score);
    if (*head == NULL) {
        *head = newNode;
    } else {
        Student *current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
        newNode->prev = current;
    }
}

// 删除学生信息
void deleteStudent(Student **head, char *name) {
    Student *current = *head;
    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            if (current->prev != NULL) {
                current->prev->next = current->next;
            } else {
                *head = current->next;
            }
            if (current->next != NULL) {
                current->next->prev = current->prev;
            }
            free(current);
            return;
        }
        current = current->next;
    }
}

// 查找学生成绩
int findStudent(Student *head, char *name) {
    Student *current = head;
    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            return current->score;
        }
        current = current->next;
    }
    return -1; // 学生不存在
}

// 打印所有学生信息
void printStudents(Student *head) {
    Student *current = head;
    while (current != NULL) {
        printf("%s %d\n", current->name, current->score);
        current = current->next;
    }
}

// 将学生信息写回文件
void saveToFile(Student *head, const char *filename) {
    FILE *file = fopen(filename, "w");
    if (file == NULL) {
        perror("Error opening file");
        exit(1);
    }
    Student *current = head;
    while (current != NULL) {
        fprintf(file, "%s %d\n", current->name, current->score);
        current = current->next;
    }
    fclose(file);
}

int main() {
    Student *head = NULL;
    char filename[] = "class_scores.txt";
    int i, j, k;
    char name[50];
    char scoreStr[10];

    head = createList(filename);

    scanf("%d", &i);
    getchar();
    for (int q = 0; q < i; q++) {
        scanf("%s", name);
        int score = findStudent(head, name);
        if (score != -1) {
            printf("%d\n", score);
        } else {
            printf("-1\n");
        }
    }

    scanf("%d", &j);
    getchar();
    for (int a = 0; a < j; a++) {
        scanf("%s %s", name, scoreStr);
        int score = atoi(scoreStr);
        addStudent(&head, name, score);
    }

    scanf("%d", &k);
    getchar();
    for (int d = 0; d < k; d++) {
        scanf("%s", name);
        deleteStudent(&head, name);
    }

    saveToFile(head, "class_scores_result.txt");

    Student *current = head;
    while (current != NULL) {
        Student *next = current->next;
        free(current);
        current = next;
    }

    return 0;
}
